package com.game.server.message.proto.execute;

import com.comm.ISession;

import com.game.majiang.config.DataManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.server.db.dao.XyClubMemberInfoDao;
import com.game.server.db.entity.UserInfoData;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.db.entity.XyClubMemberInfo;
import com.game.server.message.proto.in.AbstractXYfindJuniorPlayerReq;
import com.game.server.message.proto.out.XYclubJuniorMemberStruct;
import com.game.server.message.proto.out.XYfindJuniorPlayerRsp;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class XYfindJuniorPlayerReq extends AbstractXYfindJuniorPlayerReq {

    @Override
    public void Execute(ISession session) {
        LogicClient client = (LogicClient) LogicServer.getInstance().getClient(session.getId());
        if (client != null && client.checkClick(msgcode)) {
            int limit = 20;
            XYfindJuniorPlayerRsp rsp = new XYfindJuniorPlayerRsp();
            XyClubInfo clubInfo = DataManager.getInstance().getMapClub().get(this.clubid);
            if (clubInfo!=null){
                XyClubMemberInfo memberInfo = clubInfo.getPlayer().get(this.uid);
                if (memberInfo.getIsAccessory()==1 && memberInfo.getDel_type()==0){
                    JSONObject json = XyClubMemberInfoDao.findJuniorMemberPage(limit, this.page, this.clubid, this.uid);
                    rsp.setAllpage(json.getInt("allpage"));
                    rsp.setAllPlayercount(json.getInt("allPlayercount"));
                    rsp.setPage(this.page);
                    JSONArray array = json.getJSONArray("array");
                    XYclubJuniorMemberStruct[] juniorPlayers = new XYclubJuniorMemberStruct[array.size()];
                    int index = 0;
                    for (Object ent : array) {
                        XYclubJuniorMemberStruct juniorMemberStruct = new XYclubJuniorMemberStruct();
                        JSONObject object = JSONObject.fromObject(ent);
                        UserInfoData infoData = DataManager.getInstance().getMapUserData().get(object.getInt("uid"));
                        String strData = object.getString("strData");
                        juniorMemberStruct.setName(infoData.getName());
                        juniorMemberStruct.setHeadurl(infoData.getHead());
                        juniorMemberStruct.setUid(infoData.getId());
                        juniorMemberStruct.setDayContribute(Integer.parseInt(strData.split(",")[0]));
                        juniorMemberStruct.setDayShoushu(Integer.parseInt(strData.split(",")[1]));
                        juniorMemberStruct.setSumContribute(strData.split(",")[2]);
                        juniorPlayers[index] = juniorMemberStruct;
                        index++;
                    }
                    rsp.setJuniorPlayers(juniorPlayers);
                    rsp.setTotalBonusBalance(memberInfo.getBalance_bonus());
                    rsp.setTotalBonusAccumulation(memberInfo.getAggregate_bonus()+"");
                    rsp.setBonusShare(memberInfo.getBonus_share());
                }
            }
            client.Send(rsp);
        }
    }

}
